package com.hzj;

import java.util.List;

import org.elasticsearch.index.query.FuzzyQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;

import com.hzj.dao.ProductDao;
import com.hzj.pojo.Product;

@SpringBootTest
class DemoApplicationTests02 {
    @Autowired
    private ProductDao productDao;
    @Autowired
    ElasticsearchRestTemplate template;

    /**
     * term 查询
     * search(termQueryBuilder) 调用搜索方法，参数查询构建器对象
     */
    @Test
    public void termQuery() {
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "手");
        Iterable<Product> products = productDao.search(termQueryBuilder);
        for (Product product : products) {
            System.out.println(product);
        }
    }

    /**
     * term 查询加分页
     */
    @Test
    public void termQueryByPage() {
        int currentPage = 0;
        int pageSize = 5;
        //设置查询分页
        PageRequest pageRequest = PageRequest.of(currentPage, pageSize);
        FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("title", "手");
        Iterable<Product> products = productDao.search(fuzzyQueryBuilder, pageRequest);

        for (Product product : products) {
            System.out.println(product);
        }
    }

    //调用自定义查询
    @Test
    public void query() {
        List<Product> byPriceBetween = productDao.findByTitleLike("[机");
        for (Product product : byPriceBetween) {
            System.out.println(product);
        }

    }


}

